﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.Currency.IoUtils;
using CatUtils.Utils.Helper;
using CatUtils.Utils.SpecialUtils.View;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MusicPhotograph.VideoHelper
{
    internal class VideoTool
    {
        public readonly static string VideoSaveFloder = StaticValue.TempPath + "movie\\";
        public readonly static string MediaFloder = StaticValue.TempPath + "media\\";

        public void CreateVideo()
        {
            string FloderPath = new FloderHelper().SelectFloder();
            if (FloderPath == "")
                return;
            FloderPath = MergeVideo(FloderPath, new FloderNameHelper().GetFloderName(FloderPath));
            new MessageHelper().OpenFloderAlert(FloderPath);
        }

        /// <summary>
        /// 合并视频
        /// </summary>
        /// <param name="FloderPath"></param>
        /// <returns></returns>
        public string MergeVideo(string FloderPath, string Tip)
        {
            FloderPath = ImgToVideoBatch(FloderPath);
            List<string> FileList = new FloderNameHelper().GetFullFileNameList(FloderPath);
            string ListFile = FloderPath + "filelist.txt";
            string MergeVideo = FloderPath + Tip + ".mp4";
            if (FileList.Count == 0)
                return MergeVideo;
            PrintLog.ENoWrite("进入合并环节");
            foreach (var item in FileList)
            {
                File.AppendAllText(ListFile, string.Format("\r\nfile '{0}'", item), Encoding.ASCII);
            }

            MergeVideo = new FfmpegHelper().MergeMoive(ListFile, MergeVideo);
            string OutAudio = new FileNameHelper().CreateFileNameAddTip(MergeVideo, DateTime.Now.ToString("-MM月dd日"));
            new FfmpegHelper().AddAudio(MergeVideo, GetMedia(), OutAudio);
            foreach (var item in FileList)
            {
                new FileHelper().FileDelete(item);
            }
            //   new FileHelper().FileDelete(MergeVideo);
            new FileHelper().FileDelete(ListFile);
            return FloderPath;
        }

        /// <summary>
        /// 获取音频文件列表
        /// </summary>
        /// <returns></returns>
        private string GetMedia()
        {
            new FloderHelper().FloderExits(MediaFloder, true);
            List<string> FileList = new FloderNameHelper().GetFullFileNameList(MediaFloder);
            if (FileList.Count == 0)
                return "";
            return new ListHelper().Ramdom(FileList, true);
        }

        /// <summary>
        /// 批量转换图片到淡入淡出视频
        /// </summary>
        /// <param name="FloderPath"></param>
        /// <returns>返回视频存储文件夹</returns>
        public string ImgToVideoBatch(string FloderPath)
        {
            string TempFloder = $"{ VideoSaveFloder + DateTime.Now.ToString("yyyy-MM-ddHH-mm-ss")}\\";
            new FloderHelper().FloderExits(TempFloder, true);

            List<string> FileList = new FloderNameHelper().GetFullFileNameList(FloderPath);
            System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
            stopwatch.Start(); //  开始监视代码运行时间
            int CompltedCount = 0;
            foreach (var item in FileList)
            {
                PrintLog.E(stopwatch.Elapsed.ToString());
                new PrintLog().PercentLog(FileList.Count, ++CompltedCount);
                string SingleFile = ImageToVideo(item);
                if (SingleFile == "")
                    continue;
                new FileHelper().FileMove(SingleFile, $"{ TempFloder + DateTime.Now.ToFileTime().ToString() }.mp4");
            }
            stopwatch.Stop();
            return TempFloder;
        }

        /// <summary>
        /// 图片转换为视频 淡入淡出效果
        /// </summary>
        /// <param name="FilePath"></param>
        /// <returns></returns>
        public string ImageToVideo(string FilePath)
        {
            if (FilePath == "")
                return "";

            FilePath = new ImageMagickHelper().CreateMoiveImg(FilePath, "图片ID：");
            FilePath = new FfmpegHelper().CreateImageMoive(FilePath);
            FilePath = new FfmpegHelper().CreateFadeMoive(FilePath);

            return FilePath;
        }
    }
}